<!--
 * @Description: 
 * @Author: linjz
 * @Date: 2021-06-25 10:25:00
 * @LastEditors: linjz
 * @LastEditTime: 2021-06-28 17:22:57
-->
<template>
  <!-- 发布资讯页面 -->
  <div>
    <!-- 表单 -->
    <div>
      <!-- {{ publishForm }} -->
      <el-form
        ref="publishForm"
        :model="publishForm"
        label-width="50px"
        :rules="rules"
      >
        <el-row>
          <el-col :span="16">
            <el-form-item label="标题" prop="title">
              <el-input v-model="publishForm.title"></el-input>
            </el-form-item>
            <el-form-item label="分类" prop="categoryId">
              <el-select
                v-model="publishForm.categoryId"
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="item in categoryData"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="封皮" prop="cover">
              <el-upload
                :action="uploadFileURL"
                list-type="picture-card"
                :on-remove="handleRemove"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
                :limit="1"
              >
                <i class="el-icon-plus"></i>
                <div slot="tip" class="el-upload__tip">
                  文件大小不允许超过3M
                </div>
              </el-upload>
              <el-dialog :visible.sync="dialogVisible">
                <img width="100%" :src="imageUrl" alt="" />
              </el-dialog>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="正文" prop="content">
          <tinymce v-model="publishForm.content" :heigh="300" />
        </el-form-item>
      </el-form>
    </div>

    <!-- 按钮 -->
    <div class="btn">
      <el-button type="text" @click="$router.back()">返回</el-button>
      <el-button type="primary" @click="toPublish">发布</el-button>
    </div>
  </div>
</template>

<script>
import { findAll as queryCategory } from "@/api/infoCategory";
import { uploadFileURL, showFileURL } from "@/utils/config";
import Tinymce from "@/components/Tinymce";
import { saveOrUpdate } from "@/api/article";

export default {
  components: {
    Tinymce,
  },
  data() {
    return {
      // 表单数据
      publishForm: {},
      // 上传图片地址
      uploadFileURL,
      showFileURL,
      dialogVisible: false,
      imageUrl: "",
      // 栏目信息
      categoryData: {},
      content: {},
      rules: {
        title: [{ required: true, message: "请输入文章标题", trigger: "blur" }],
        categoryId: [
          { required: true, message: "请选择栏目", trigger: "change" },
        ],
        content: [
          { required: true, message: "请输入文章内容", trigger: "blur" },
        ],
      },
    };
  },
  computed: {},
  methods: {
    // 删除图片的回调函数
    handleRemove(file, fileList) {
      // console.log(file, fileList);
      this.publishForm.cover = "";
      // console.log(this.publishForm);
    },
    // 图片上传成功后的回调函数
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.publishForm.cover = showFileURL + res.data.id;
    },
    // 图片上传之前的回调函数
    beforeAvatarUpload(file) {
      const isLt3M = file.size / 1024 / 1024 < 3;
      if (!isLt3M) {
        this.$message.error("上传头像图片大小不能超过 3MB!");
      }
      return isLt3M;
    },
    // 获取栏目信息
    async queryCategoryData() {
      let res = await queryCategory();
      // console.log(res);
      this.categoryData = res.data;
    },
    toPublish() {
      this.$refs.publishForm.validate(async (valid) => {
        if (valid) {
          const res = await saveOrUpdate(this.publishForm);
          // console.log(res);
          if (res.status === 200) {
            this.$router.push({ path: "List" });
            this.$notify({
              title: "成功",
              message: res.message,
              type: "success",
            });
          }
        } else {
          return false;
        }
      });
    },
  },
  created() {
    this.queryCategoryData();
  },
  mounted() {},
};
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.btn {
  float: right;
}
</style>